'use strict';
(function (exports) {
    var $ = null;

    var PhzZongJieSuan = cc.Layer.extendEX({
        getRootNode: function () {
            return this.getChildByName('Layer');
        },
        ctor: function (data) {
            this._super();
            // activityZhongqiu(gameData.parent_area, gameData.roomId);
            var that = this;

            $ = create$(loadNodeCCS_old(res.PhzZongJieSuan_json, this));

            this.share = $('root.share');
            this.close = $('root.close');
            this.title = $('root.title');
            this.txtTime = $('root.txtTime');
            this.txtRoomId = $('root.txtRoomId');
            this.txtRounds = $('root.txtRounds');
            this.title.setString(mRoom.getWanfaName(mRoom.wanfatype));
            if (window.inReview) {
                this.share.setVisible(false);
                this.close.setPositionX(cc.winSize.width / 2);
            }

            // 周年庆
            if (activity && activity.gameOverCallback)
                activity.gameOverCallback(gameData.area, gameData.roomId, gameData.uid);

            playEffect('yin_win');
            mRoom.isClean = true;
            gameData.roomId = 0;
            //关闭按钮 5秒后 才可以点击
            TouchUtil.setOnclickListener(that.close, function (node) {
                mRoom.isClean = false;
                gameData.roomId = 0;
                //cc.eventManager.removeAllListeners();
                gameData.wanfatype = '';
                if (window.nativeVersion > '3.5.0') {
                    clearGameMWRoomId();
                }

                showBoard('HallBoard');
            });
            TouchUtil.setOnclickListener(that.share, function (node) {
                that.share.setVisible(false);
                that.close.setVisible(false);
                that.scheduleOnce(function () {
                    that.share.setVisible(true);
                    that.close.setVisible(true);
                }, 1);
                if (!cc.sys.isNative)
                    return;
                ShareUtil.SharePic(that);
            });
            $('root.adv').setVisible(false);
            if (gameData.switchVideo) {
                AgoraUtil.closeVideo();
            }

            return true;
        },
        setRoom: function (resultlayer, gameOverData) {
            this.gameOverData = gameOverData;

            //格式化时间
            this.txtTime.setString(this.gameOverData.EndTime);
            this.txtRoomId.setString('房间号:' + this.gameOverData.RoomID);
            this.txtRounds.setString('局数:' + this.gameOverData.CurrentRound + '/' + this.gameOverData.TotalRound);
            this.setData();
            if (!window.inReview && this.gameOverData['CurrentRound'] > 3 && gameData.newyear) {
                var timemiao = Date.parse(new Date(this.gameOverData)) / 1000;
                activityRedRain(timemiao);
            }

            //两人玩 BUG
            var players = [];
            for (var i = 0; i < gameData.players.length; i++) {
                var player = gameData.players[i];
                if (player.uid != 0) {
                    players.push(player);
                }
            }

            //中秋活动
            if (gameData.opt_conf.zhongqiu) {
                var maxScore = 0;
                var myScore = 0;
                for (var i = 0; i < players.length; i++) {
                    var player = players[i];
                    var uid = player['uid'];
                    if (maxScore < player['score']) {
                        maxScore = player['score'];
                    }
                    if (gameData.uid == uid) {
                        myScore = player['score'];
                    }
                }
                if (myScore == maxScore) {
                    var roomID = this.gameOverData.RoomID;
                    var _ts = this.gameOverData.Endtime.low;
                    if (!roomID && !!gameData.last3002)
                        roomID = gameData.last3002.room_id;
                    if (this.gameOverData.CurrentRound == this.gameOverData.TotalRound && gameData.opt_conf.zhongqiu == 1) {
                        activityZhongqiu(gameData.parent_area, roomID, _ts, gameData.uid);
                    } else if (gameData.opt_conf.zhongqiu == 2) { // 测试不需要打满
                        activityZhongqiu(gameData.parent_area, roomID, _ts, gameData.uid);
                    }
                }
            }
        },
        setData: function () {
            var maxScore = -100;
            // 耒阳字牌新增二人玩，此时判断时报错。
            // if (this.gameOverData.Users[0].Score == 0
            //     && this.gameOverData.Users[1].Score == 0
            //     && this.gameOverData.Users[2].Score == 0){
            //     maxScore = 9999;//都是0,没有赢家
            // }else{
            //     for(var s = 0; s < this.gameOverData.Users.length; s++){
            //         var score = this.gameOverData.Users[s].Score;
            //         if(score > maxScore){
            //             maxScore = score;
            //         }
            //     }
            // }
            //--->改为如下
            for (var s = 0; s < this.gameOverData.Users.length; s++) {
                var score = this.gameOverData.Users[s].Score;
                if (score > maxScore) {
                    maxScore = score;
                }
            }
            if (maxScore == 0) {
                maxScore = 9999; //都是0,没有赢家
            }

            if (gameData.playerNum == 3) {
                var info4 = $('root.info4');
                info4.setVisible(false);
                for (var i = 0; i < 3; i++) {
                    var info = $('root.info' + (i + 1));
                    var positionx = info.getPositionX();
                    //info.setPositionX(50 + i*392 + (1-i)*40);
                    info.setPositionX(positionx + $('root.info' + (i + 1) + '.Image').getContentSize().width / 4 * (i + 1));
                }
            } else if (gameData.playerNum == 2) {
                var info4 = $('root.info4');
                info4.setVisible(false);
                var info3 = $('root.info3');
                info3.setVisible(false);
                for (var i = 0; i < 2; i++) {
                    var info = $('root.info' + (i + 1));
                    var positionx = info.getPositionX();
                    info.setPositionX(positionx + $('root.info' + (i + 1) + '.Image').getContentSize().width / 2 * (2 * i + 1));
                }
            }
            var pList = this.gameOverData.Users;
            var fangzhuIsIn = false;
            for (var i = 0; i < pList.length; i++) {
                var player = pList[i];

                var info = $('root.info' + (i + 1));
                var name = $('name', info);
                var id = $('id', info);
                var score = $('score', info);
                var score_bg = $('score_back', info);
                var winner = $('winner', info);
                // var hpcs =  $("hpcs", info);
                // var zghx =  $("zghx", info);
                // var zgfx =  $("zgfx", info);
                // var zgjf =  $("zgjf", info);
                var icon_fz = $('icon_fz', info);
                if (icon_fz != undefined && player.UserID == mRoom.ownner) {
                    icon_fz.setVisible(true);
                } else if (icon_fz != undefined) {
                    icon_fz.setVisible(false);
                }
                name.setString(ellipsisStr(decodeURIComponent(player.UserName), 6) || '');
                id.setString(player.UserID ? ('ID:' + player.UserID) : '');
                var playerscore = player.Score || '0';
                if (playerscore < 0) {
                    score_bg.loadTexture(huzi.resPath+'dynamicRes/' + 'roomclean/back3.png');
                } else {
                    score_bg.loadTexture(huzi.resPath+'dynamicRes/' + 'roomclean/back2.png');
                }
                //头像
                var avatorbg = $('avator', info);
                var avator = avatorbg.getChildByName('head');
                if (avator == null) {
                    avator = new cc.Sprite(huzi.res.transparent_97x99_png);
                    avator.setPosition(avatorbg.getContentSize().width / 2, avatorbg.getContentSize().height / 2);
                    //avator.setScale(0.8);
                    avator.setName('avator');
                    avatorbg.addChild(avator, -1);
                }
                var url = decodeURIComponent(player.HeadImgURL);
                if (url == undefined || (url.length == 0)) url = 'res/image/defaultHead.jpg';
                loadImageToSprite(url, avator);//头像

                if (playerscore >= maxScore) {
                    winner.setVisible(true);
                    winner.setTexture(huzi.resPath+'dynamicRes/' + 'roomclean/winner.png');
                } else {
                    winner.setVisible(false);
                }

                if (this.gameOverData.Owner && this.gameOverData.Owner == player.UserID) {
                    fangzhuIsIn = true;
                }

                var scoreSView = $('scoreSView', info);

                if (player.Scores && scoreSView) {

                    if (playerscore && playerscore >= 0) {
                        if (playerscore == 0) {
                            score.setString('+' + playerscore);
                        } else {
                            playerscore = '+' + playerscore;
                            score.setString(playerscore);
                        }
                    } else {
                        score.setString(playerscore);
                    }

                    var length = player.Scores.length;
                    var row0 = $('scoreSView.row0', info);
                    var innerHeight = 0;
                    if (row0)
                        (row0.getContentSize().height + 15) * length;
                    // scoreSView.setInnerContainerSize(210, innerHeight);
                    if (innerHeight < 280) {
                        innerHeight = 280;
                    }
                    // scoreSView.setInnerContainerSize(210, innerHeight);
                    var delay = scoreSView.getContentSize().height - innerHeight;
                    for (var j = 0; j < length; j++) {
                        var node = $('scoreSView.row' + j, info);
                        if (!node) {
                            node = duplicateLayout(row0);
                            node.setName('row' + j);
                            row0.getParent().addChild(node);
                        }
                        node.setPositionY(innerHeight - (row0.getContentSize().height + 5) * (j + 1) + (delay > 0 ? delay : 0));
                        var name = $('name', node);
                        var score_1 = $('score', node);
                        var itembg = $('itembg', node);
                        // if(j%2 == 0){
                        //     itembg.setVisible(true);
                        // } else {
                        //     itembg.setVisible(false);
                        // }
                        name.setString('第' + (j + 1) + '局');
                        var _score = player.Scores[j];
                        _score = _score >= 0 ? ('+' + _score) : _score;
                        score_1.setString(_score);
                    }

                    scoreSView.innerHeight = innerHeight;
                    id.setVisible(true);
                } else {
                    id.setVisible(true);
                }
            }
            var fangzhu = $('root.icon_fz_1');
            if (fangzhu) fangzhu.setVisible(fangzhuIsIn);
        }
    });
    exports.PhzZongJieSuan = PhzZongJieSuan;
})(window);